//
//  CJOccputionAuthenticationController.m
//  Boss
//
//  Created by admin on 2018/9/13.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJOccputionAuthenticationController.h"
#import "WYUploadModel.h"
#import "UIImage+WYSaveImage.h"
#import "CJUploadAuthenticationSucessController.h"
@interface CJOccputionAuthenticationController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>


@property (nonatomic,strong)NSString *pathString;

@property (nonatomic,strong)WYUploadModel *model;


@property (strong, nonatomic) IBOutlet UIImageView *positiveImageView;

@property (strong, nonatomic) IBOutlet UIImageView *negativeImageView;

@property (nonatomic,strong) NSString *positiveString;
@property (nonatomic,strong) NSString *negativeString;

@property (nonatomic)NSUInteger type;

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) IBOutlet UIView *mainView;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;

@property (strong, nonatomic) IBOutlet UILabel *occupationName;

@property (strong, nonatomic) IBOutlet UIImageView *IDCardImageView;
@property (nonatomic,strong) NSString *IDCardString;

@property (strong, nonatomic) IBOutlet UIImageView *proveImageView;
@property (nonatomic,strong) NSString *proveImageString;


@property (strong, nonatomic) IBOutlet UIImageView *licenseImageView;
@property (nonatomic,strong) NSString *licenseString;

@property (strong, nonatomic) IBOutlet UITextField *nameTextField;

@property (strong, nonatomic) IBOutlet UITextField *zhiweiTextField;

@property (strong, nonatomic) IBOutlet UITextField *companyTextField;


@property (nonatomic,strong) NSString *upLoadString;
@property (nonatomic,strong) NSString *upLoadTag;



@end

@implementation CJOccputionAuthenticationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.topHeight.constant = self.navHeight;
}
-(void)setupUI
{
    self.navigationItem.title = @"职业认证";
    self.mainView.width = kScreenWidth;
    self.mainView.height = 930;
    
    [self.scrollView addSubview:self.mainView];
    self.scrollView.contentSize = self.mainView.size;
    self.IDCardImageView.userInteractionEnabled = YES;
    self.proveImageView.userInteractionEnabled = YES;
    self.licenseImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(IDCardImageViewAction:)];
    [self.IDCardImageView addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(proveImageViewAction:)];
    [self.proveImageView addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(licenseImageViewAction:)];
    [self.licenseImageView addGestureRecognizer:tap3];
}
-(void)IDCardImageViewAction:(UITapGestureRecognizer *)ges
{
    self.type = 3;
    [self showPicker];
}
-(void)proveImageViewAction:(UITapGestureRecognizer *)ges
{
    self.type = 4;
    [self showPicker];
}
-(void)licenseImageViewAction:(UITapGestureRecognizer *)ges
{
    self.type = 5;
    [self showPicker];
}

- (IBAction)positiveAction:(id)sender {
    self.type = 1;
    [self showPicker];
    
}
- (IBAction)negativeAction:(id)sender {
    self.type = 2;
    [self showPicker];
    
    
}

-(void)showPicker
{
    
    WEAKSELF
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self; //设置代理
  //  imagePickerController.allowsEditing = YES;
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];
    }];
    
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    if (IS_IPAD) {
        //        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        //        popPresenter.sourceView = cell;
        //        popPresenter.sourceRect = cell.bounds;
        //        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //[self showLoading:@""];
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    NSData *data = UIImageJPEGRepresentation(photo, 0.2);
    
    UIImage *photo1 = [UIImage imageWithData:data];
    
    
    
    
    
    if (self.type==1) {
        self.positiveImageView.image  = photo1;
    }else if(self.type==2){
        self.negativeImageView.image  = photo1;
    }else if (self.type==3){
        self.IDCardImageView.image = photo1;
    }else if (self.type == 4){
        self.proveImageView.image = photo1;
    }else if (self.type ==5){
        self.licenseImageView.image = photo1;
    }
    
    
    NSString *path = [photo1 QCloudTempFilePathWithExtension:@"PNG"];
    self.pathString = path;
    [UIImagePNGRepresentation(photo1) writeToFile:path atomically:YES];
    
    [self showLoading:@""];
    
    WEAKSELF
    [WYHttpServer uploadImageSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        WYUploadModel *model = [WYUploadModel getupLoadModelFromJson:response[@"result"]];
        [weakSelf upLoadWithPath:model.uploadUrl];
        weakSelf.model = model;
        
        
    }];
}
-(void)upLoadWithPath:(NSString *)path
{
    WEAKSELF
    [WYHttpServer PUTWithURLStr:path filePath:self.pathString SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
          
            [weakSelf hideLoading];
        });
        if (success) {
            if (self.type==1) {
                self.positiveString  = self.model.visitUrl;
            }else if(self.type==2){
                self.negativeString  = self.model.visitUrl;
            }else if (self.type==3){
                self.IDCardString = self.model.visitUrl;
                self.upLoadString = self.model.visitUrl;
                self.upLoadTag = @"1";
            }else if (self.type == 4){
                self.proveImageString = self.model.visitUrl;
                self.upLoadString = self.model.visitUrl;
                self.upLoadTag = @"2";
            }else if (self.type ==5){
                self.licenseString = self.model.visitUrl;
                self.upLoadString = self.model.visitUrl;
                self.upLoadTag = @"3";
            }
            // [weakSelf editHeaderImageview];
        }else{
            [weakSelf toast:errorMessage];
        }
        
    }];
}


- (IBAction)upLoadAction:(id)sender {
    if (!self.nameTextField.text.length) {
        [self toast:@"请输入姓名"];
    }
    if (!self.zhiweiTextField.text.length) {
        [self toast:@"请输入职位"];
    }
    if (!self.companyTextField.text.length) {
        [self toast:@"请输入公司全称"];
    }
    
    if (!self.positiveString.length) {
         [self toast:@"请添加身份证正面"];
    }
    if (!self.negativeString.length) {
        [self toast:@"请添加身份证反面"];
    }
    if (!self.upLoadString.length) {
        [self toast:@""];
    }
    WEAKSELF
    [WYHttpServer sendFirmAuthenticationMessageWithUserName:self.nameTextField.text WithPositon:self.zhiweiTextField.text WithFirmName:self.companyTextField.text WithIdCardFront:self.positiveString WithIdCardBack:self.negativeString WithVerifyType:@"1" WithVerify:self.upLoadString SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            CJUploadAuthenticationSucessController *uasc = [[CJUploadAuthenticationSucessController alloc]init];
            [self.navigationController pushViewController:uasc animated:YES];
            
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        }
        
        
        
        
        
    }];
    
    
    
    
    
    
    
    
    
}







@end
